<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Contatos extends CI_Controller {

	function __construct() {
		parent::__construct();
		/*if(!$this->session->userdata('grambel')){
			$this->session->sess_destroy();
			redirect('login', 'refresh');
		}*/
	}
	public function index(){
		if(!$this->session->userdata('grambel')){
			$this->session->sess_destroy();
			redirect('login', 'refresh');
		}
		
		$secretaria = new Secretaria();
		$data["secretaria"] = $secretaria->consultaSecretaria();
		
		$this->load->view('header');
		$this->load->view('view_contato', $data);
	}
	public function novo(){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		temPermissao(array('1','2'),$session_data);
		
		$secretaria = new Secretaria();
		$data["secretaria"] = $secretaria->consultaSecretaria();
		
		$this->load->view('header');
		$this->load->view('view_contato', $data);
		
	}
	public function salvar(){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		temPermissao(array('1','2'),$session_data);
		
		if($this->input->post()){
		
			$contato = new Contato();
			
			$contato->nomecontato = strtoupper($this->input->post("contato"));
			$contato->celular = ($this->input->post("celular"));
			$contato->cargo = strtoupper($this->input->post("cargo"));
			$contato->email = $this->input->post("email");
			$contato->tag = strtoupper($this->input->post("tag"));
			
			$ramal = new Ramal();
			$ramal->where("id",$this->input->post("idramal"))->get();
		
			if($contato->save($ramal)){
				$acao = $contato->check_last_query(false,true);
				$log = new Log();
				$log->ip = $this->session->userdata('ip_address');
				$log->usuario = $session_data["nome"];
				$log->acao = $acao;
				$log->save();
			
				echo 'CONTATO CADASTRADO';
			}else{
				echo $contato->error->string;
			
			}
		
		
		}else{
		
			echo 'ERROR - EMPTY';
		
		}
	
	}
	public function editar($contato_id = null, $ramal_id = null){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		temPermissao(array('1','2'),$session_data);
		
	
		if($this->input->post()){
			$contatod  = new Contato();
			$contatod->where("id", $this->input->post("idcontato"))->get();
			
			if($contatod->exists()){
				
				$contatod->delete();
				
				$acao = $contatod->check_last_query(false,true);
				$log = new Log();
				$log->ip = $this->session->userdata('ip_address');
				$log->usuario = $session_data["nome"];
				$log->acao = $acao;
				$log->save();
				
				$contato  = new Contato();
				
				$ramal = new Ramal();
				$ramal->where("id",$this->input->post("idramal"))->get();
				
				$contato->nomecontato = strtoupper($this->input->post("contato"));
				$contato->celular = ($this->input->post("celular"));
				$contato->cargo = strtoupper($this->input->post("cargo"));
				$contato->email = $this->input->post("email");
				$contato->tags = strtoupper($this->input->post("tags"));
				
				if($contato->save($ramal)){
					
					$acao = $contato->check_last_query(false,true);
					$log = new Log();
					$log->ip = $this->session->userdata('ip_address');
					$log->usuario = $session_data["nome"];
					$log->acao = $acao;
					$log->save();
			
					echo 'CONTATO ATUALIZADO';
				}else{
					echo $contato->error->string;
				}
			}
		}else{
			$contato  = new Contato();
			//CARREGA VIEW_RAMAL_EDITA
			$contato->include_related("ramal")->where("ramais.id",$ramal_id)->where("contatos.id",$contato_id)->get();
			//echo $contato->check_last_query();
			if($contato->exists()){
				$data["contato"] = $contato;
				$this->load->view('header');
				$this->load->view('view_contato_edita',$data);
		
			}else{
				echo "<script>alert('CONTATO NAO POSSUI RAMAL VINCULADO!');</script>";
			}
		}
	}
	public function editarcontatos($id =null){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		temPermissao(array('1','2'),$session_data);
		
		if($this->input->post()){
		
			$contatos = implode(",", $this->input->post("contatos"));
			$contato  = new Contato();
			echo "CONTATOS ALTERADOS";
			$contato->query("UPDATE contatos_ramais SET ramal_id='".$this->input->post("idramal")."' WHERE contato_id IN (".$contatos.")");
			
			$acao = $contato->check_last_query(false,true);
			$log = new Log();
			$log->ip = $this->session->userdata('ip_address');
			$log->usuario = $session_data["nome"];
			$log->acao = $acao;
			$log->save();
			
		}else{
			$contato  = new Contato();
			$contato->include_related("ramal")->where("ramais.id", $id)->get();
			if($contato->exists()){
				$secretaria = new Secretaria();
				$data["secretaria"] = $secretaria->consultaSecretaria();
				$data["contatos"] = $contato;
				$this->load->view('header');
				$this->load->view('view_contatos_edita', $data);
			}else{
				$this->load->view('header');
				$this->load->view('view_contato_consulta');
			
			}
		}
		
	}
	public function excluir($contato_id = null){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		temPermissao(array('1','2'),$session_data);
		
		if(!empty($contato_id)){
			$contato = new Contato();
			$contato->where("id",$contato_id)->get();
			//echo $ramal->check_last_query();
			
			if($contato->exists()){
				
				$contato->delete();
				$acao = $contato->check_last_query(false,true);
				$log = new Log();
				$log->ip = $this->session->userdata('ip_address');
				$log->usuario = $session_data["nome"];
				$log->acao = $acao;
				$log->save();
			
				echo "<script>alert('CONTATO EXCLUIDO!');</script>";
				echo "<script>window.location.href = '".base_url('index.php/contatos/consulta/')."';</script>";
				
			}
		}
	
	}
	public function excluirSelecionados(){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		temPermissao(array('1','2'),$session_data);
		
		if($this->input->post("id")){
			$msg = '';
			foreach($this->input->post("id") as $cont){
				$contato = new Contato();
				$contato->where("id",$cont)->get();
				
				if($contato->exists()){
					
					$contato->delete();
					$acao = $contato->check_last_query(false,true);
					$log = new Log();
					$log->ip = $this->session->userdata('ip_address');
					$log->usuario = $session_data["nome"];
					$log->acao = $acao;
					$log->save();
				
					$msg .= "CONTATO ".$cont." EXCLUIDO\n";
					
				}
			}
			echo $msg;
		}else{
			echo "SELECIONE PELO MENOS UM CONTATO";
		}
		
	}
	public function editamassa(){
		if($this->input->post()){
			$contatos = $this->input->post("item");
			$contato = new Contato();
			$contato->include_related("ramal")->where_in("id",$contatos)->get();
			//echo $contato->check_last_query();
			
			$data["contatos"] = $contato;
			$this->load->view('header');
			$this->load->view('view_contatos_edita', $data);
			
		}else{
		
			echo 'ERROR - EMPTY';
		
		}
	}
	public function consulta(){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		temPermissao(array('1','2'),$session_data);
		/*
		$contato = new Contato();
		$contato = $contato->consultaContatosAll();
		$data["contatos"] = $contato;
		*/
		$this->load->view('header');
		$this->load->view('view_contato_consulta');
	
	}
	public function consultaContatosJson(){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		temPermissao(array('1','2'),$session_data);
		
		$contato = new Contato();
		echo $contato->consultaContatosAll();
	
	}
	public function valida(){
	
		$contato = new Contato();
		$contato->include_related('ramal')->where("nomecontato",$this->input->get("contato"))->where('ramais.id',$this->input->get("idramal"))->get();
		
		//echo $contato->check_last_query();
		
		if($contato->exists()){ 
			echo "false";
		}else{ 
			echo "true";
		}
	
	}
	public function consultaRapida(){
		
		if($this->input->post('word')){
			$string = $this->input->post('word');
			
			echo '<br/>';
			
			$contato = new Contato();
			$r = new Contato();
			
			$r = $contato->consultaContato($string);
			if($r->exists()){
				foreach($r as $dados){
				
					echo '<div class="panel panel-default">';
					echo '<div class="panel-body">';
					echo '<div class="row">
					<div class="col-xs-6" style="font-size: 16px;font-weight: bold;">'.$dados->nomecontato.'</div>
					<div class="col-xs-6">'.$dados->cargo.'</div>
					</div>';
					echo '<div class="row">
					<div class="col-xs-6">'.$dados->email.'</div>
					<div class="col-xs-6"></div>
					</div>';
					echo '<div class="row">
					<div class="col-xs-6" style="color: #0B792E">'.$dados->sigla.' - '.$dados->nomesetor.'</div>
					<div class="col-xs-6" style="font-size: 16px;font-weight: bold;color: #0070BB">'.$dados->ramal.'</div>
					<div class="col-xs-6"><a href="'.base_url('index.php/contatos/editar/'.$dados->contato_id).'" class="btn btn-success btn-xs" data-target="#myModal"><span class="glyphicon glyphicon-edit"></span> Editar Contato</a></div>
					<div class="col-xs-6"><a href="'.base_url('index.php/ramais/editar/'.$dados->ramal_id).'" class="btn btn-primary btn-xs" data-target="#myModal"><span class="glyphicon glyphicon-edit"></span> Editar Ramal</a></div>
					</div>';
					echo '</div></div>';
			
				}
			}else{
				echo '<div class="panel panel-default"><div class="panel-body">';
				echo '<div class="row">
				<div class="col-xs-6" style="font-size: 16px;font-weight: bold;">REGISTRO NÃO ENCONTRADO!!</div>
				<div class="col-xs-6"></div>
				</div>';
				echo '</div></div>';
			}
		}
	}
	public function consultaDefault(){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		

		if($this->input->post('term')){
			$string = $this->input->post('term');

			
		
			$contato = new Contato();
			$r = new Contato();
			
			$r = $contato->consultaContato($string);
			if($r->exists()){
				foreach($r as $dados){
					echo '<div class="panel panel-default">';
						echo '<div class="panel-body">';
							echo '<div class="row">
							<div class="col-xs-6" style="font-size: 16px;font-weight: bold;">'.strtoupper($dados->nomecontato).'</div>
							<div class="col-xs-6">'.strtoupper($dados->cargo).'</div>
							</div>';
							echo '<div class="row">
							<div class="col-xs-6">'.$dados->email.'</div>
							<div class="col-xs-6"></div>
							</div>';
							echo '<div class="row">';
							echo '<div class="col-xs-6" style="color: #0B792E">'.strtoupper($dados->sigla).' - '.strtoupper($dados->nomesetor).'</div>';
							//echo '<div class="col-xs-6" style="font-size: 16px;font-weight: bold;color: #0070BB">'.$dados->ramal.'</div>';
					if(viewPermissao(array('1','2'),$session_data)){
						echo '<div class="col-xs-6" style="font-size: 16px;font-weight: bold;color: #0070BB">'.$dados->ramal.' / <span style="color:red;">'.$dados->celular.'</span></div>';
						echo '<div class="col-xs-6"><a href="'.base_url('index.php/contatos/editar/'.$dados->contato_id.'/'.$dados->ramal_id).'" class="btn btn-success btn-xs" data-target="#myModal"><span class="glyphicon glyphicon-edit"></span> Editar Contato</a></div>';
						echo '<div class="col-xs-6"><a href="'.base_url('index.php/ramais/editar/'.$dados->ramal_id).'" class="btn btn-primary btn-xs" data-target="#myModal"><span class="glyphicon glyphicon-edit"></span> Editar Ramal</a></div>';
					}
					echo '<div class="col-xs-6" style="font-size: 16px;font-weight: bold;color: #0070BB">'.$dados->ramal.'</div>';
					echo '</div>';
					echo '</div></div>';
				}
			}else{
				echo '<div class="panel panel-default"><div class="panel-body">';
				echo '<div class="row">
				<div class="col-xs-6" style="font-size: 16px;font-weight: bold;">REGISTRO NÃO ENCONTRADO!!</div>
				<div class="col-xs-6"></div>
				</div>';
				echo '</div></div>';

				//$data [] = array("value" =>$string);
			}
			//echo '<pre>';
			//echo json_encode($data);
			
			//echo '</pre>';
			
		}
	}
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */